package com.zbcz.crm.adapter


import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.zbcz.crm.bean.ProjectFileBean
import com.zbcz.crm.databinding.ItemProjectFileListBinding
import com.zbcz.crm.util.setRoundedCorners

class ProjectFileListAdapter(private val dataList: List<ProjectFileBean>) :
    RecyclerView.Adapter<ProjectFileListAdapter.ProjectFileHolder>() {

    private val expandedStates = mutableMapOf<Int, Boolean>()

    inner class ProjectFileHolder(private val binding: ItemProjectFileListBinding) :
        RecyclerView.ViewHolder(binding.root) {

        init {
            binding.tvFold.setOnClickListener {
                toggleItemExpansion(adapterPosition)
            }
            binding.ivFold.setOnClickListener {
                toggleItemExpansion(adapterPosition)
            }

        }


        fun bind(data: ProjectFileBean) {
            binding.run {
                tvProjectFileNameValue.text = data.fileName
                tvProjectFileUploaderValue.text = data.uploadName
                tvProjectFileUploadTime.text = data.uploadTime

            }
            binding.contentParent.setRoundedCorners(45f)
        }

        fun setExpanded(expanded: Boolean) {
            val visible = if (expanded) View.VISIBLE else View.GONE
            binding.clProjectFileUploadTime.visibility = visible

        }
    }

    private fun toggleItemExpansion(position: Int) {
        expandedStates[position] = !(expandedStates[position] ?: true)
        notifyItemChanged(position)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ProjectFileHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = ItemProjectFileListBinding.inflate(inflater, parent, false)
        return ProjectFileHolder(binding)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: ProjectFileHolder, position: Int) {
        holder.bind(dataList[position])
        // 设置展开或折叠的状态
        holder.setExpanded(expandedStates[position] ?: true)
    }
}